﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class ChargesDetailBLL
    {
        private static readonly IBillingEntityDA billingEntityDA = DataAccess.CreateBillingEntityDA();
        private static readonly ICalendarGroupDetailDA calendarGroupDetailDA = DataAccess.CreateCalendarGroupDetailDA();
        private static readonly IChargesDetailDA chargesDetailDA = DataAccess.CreateChargesDetailDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IInvBackUpDA invBackUpDA = DataAccess.CreateInvBackUpDA();
        private static readonly IInvTransDA InvTransDA = DataAccess.CreateInvTransDA();
        private static readonly IPackDA iPackDA = DataAccess.CreatePackDA();
        private static readonly ILotAttributeDA LotAtributeDA = DataAccess.CreateLotAttributeDA();
        private PubParasInfo pubParasInfo;
        private static readonly IRateDetailDA RateDetailDA = DataAccess.CreateRateDetailDA();
        private static readonly ISkuDA SkuDA = DataAccess.CreateSkuDA();
        private static readonly ITariffDA TariffDA = DataAccess.CreateTariffDA();

        public ChargesDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public ChargesDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public ChargesDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string BillingBackUp(string ExecuteDate)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                if ((this.dataBaseID != null) && (this.dataBaseID.Count > 0))
                {
                    for (int i = 0; this.dataBaseID.Count > i; i++)
                    {
                        if (invBackUpDA.GetInvBackUpByID(dataBase, tran, ExecuteDate, this.dataBaseID[i]) != null)
                        {
                            invBackUpDA.DeleteInvBackUp(dataBase, tran, ExecuteDate, this.dataBaseID[i]);
                        }
                        invBackUpDA.InvBackUpDateByInvLocLotLpn(dataBase, tran, ExecuteDate, this.dataBaseID[i]);
                    }
                    tran.Commit();
                    return temp;
                }
                return "初始化仓库失败！";
            }
            catch (Exception ex)
            {
                tran.Rollback();
                temp = "库存备份异常！" + ex.Message;
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public string BillingClosingStock(string ExecuteDate, List<string> strWhID)
        {
            string temp = string.Empty;
            if ((this.dataBaseID == null) || (this.dataBaseID.Count <= 0))
            {
                throw new Exception("初始化仓库失败！");
            }
            for (int i = 0; this.dataBaseID.Count > i; i++)
            {
                if ((strWhID.Count <= 0) || strWhID.Contains(this.dataBaseID[i]))
                {
                    List<ChargesDetailInfo> chargesDetailInfoList = new List<ChargesDetailInfo>();
                    try
                    {
                        List<InvBackUpInfo> InvBackUpList = invBackUpDA.GetInvBackUpByExecuteDate(ExecuteDate, this.dataBaseID[i]);
                        foreach (InvBackUpInfo info in InvBackUpList)
                        {
                            SkuInfo skuinfo = SkuDA.GetSkuByID(info.OwnerID, info.SkuID, this.dataBaseID[i]);
                            if (skuinfo == null)
                            {
                                throw new Exception(this.WhConvert(this.dataBaseID, i) + "@未找到货主" + info.OwnerID + " 相关货品" + info.SkuID + ",业务组织失败!");
                            }
                            string strRateID = string.Empty;
                            TariffInfo tariffInfo = TariffDA.GetTariffByID(skuinfo.TariffID, this.dataBaseID[i]);
                            if (tariffInfo == null)
                            {
                                throw new Exception(this.WhConvert(this.dataBaseID, i) + "@货品 " + skuinfo.SkuID + " 未设置价目表!");
                            }
                            if (tariffInfo.TariffType == TariffType.CalendarGroup)
                            {
                                List<CalendarGroupDetailInfo> calendarGroupDetailInfo = calendarGroupDetailDA.GetCalendarGroupDetailByID(tariffInfo.TypeCode, this.dataBaseID[i]);
                                if ((calendarGroupDetailInfo == null) || (calendarGroupDetailInfo.Count <= 0))
                                {
                                    throw new Exception(this.WhConvert(this.dataBaseID, i) + "@未找到货品 " + skuinfo.SkuID + "相关的日历组 编号 " + tariffInfo.TypeCode + " 请核对日历组!");
                                }
                                DateTime ExecuteTime = Convert.ToDateTime(ExecuteDate);
                                foreach (CalendarGroupDetailInfo cgInfo in calendarGroupDetailInfo)
                                {
                                    if (((cgInfo.StartTime <= ExecuteTime) && (cgInfo.EndTime >= ExecuteTime)) && (cgInfo.IsActive == "Y"))
                                    {
                                        strRateID = cgInfo.RateID;
                                        break;
                                    }
                                }
                                if (string.IsNullOrEmpty(strRateID))
                                {
                                    throw new Exception(this.WhConvert(this.dataBaseID, i) + "@未找到日历组 编号" + tariffInfo.TypeCode + "内与当前月份相匹配的计费方式! ");
                                }
                            }
                            else if (tariffInfo.TariffType == TariffType.Fixed)
                            {
                                strRateID = tariffInfo.TypeCode;
                            }
                            List<RateDetailInfo> rateDetailInfoList = RateDetailDA.GetRateDetailByID(strRateID, this.dataBaseID[i]);
                            if ((rateDetailInfoList == null) || (rateDetailInfoList.Count <= 0))
                            {
                                throw new Exception(this.WhConvert(this.dataBaseID, i) + "@价目表" + skuinfo.TariffID + " 未找到对应的费率 ");
                            }
                            foreach (RateDetailInfo rateInfo in rateDetailInfoList)
                            {
                                if ((rateInfo.RateType == RateChargeType.Inventory) && (rateInfo.IsActive == "Y"))
                                {
                                    decimal variable = 0M;
                                    if (rateInfo.ChargeMode == RateBillingMode.Fixed)
                                    {
                                        variable = this.CountChargeUnit(rateInfo.ChargeUnit, info.Qty, skuinfo, rateInfo.ChargePerUnit, this.dataBaseID[i]) * rateInfo.FixedRate;
                                    }
                                    else if (rateInfo.ChargeMode == RateBillingMode.ChargeUnitLevel)
                                    {
                                        if (rateInfo.DistMatchedMode == RateDistMatchedMode.MIN_CONDITION_MAX)
                                        {
                                            variable = this.ChargeUnitLevelByMax(rateInfo, skuinfo, info.Qty, this.dataBaseID[i]);
                                        }
                                        if (rateInfo.DistMatchedMode == RateDistMatchedMode.MINCONDITIONMAX)
                                        {
                                            variable = this.ChargeUnitLevelByMin(rateInfo, skuinfo, info.Qty, this.dataBaseID[i]);
                                        }
                                    }
                                    else if (rateInfo.ChargeMode == RateBillingMode.StcTimeLevel)
                                    {
                                        if (rateInfo.DistMatchedMode == RateDistMatchedMode.MIN_CONDITION_MAX)
                                        {
                                            variable = this.StcTimeLevelMax(rateInfo, skuinfo, info.Lot, info.Qty, ExecuteDate, this.dataBaseID[i]);
                                        }
                                        if (rateInfo.DistMatchedMode == RateDistMatchedMode.MINCONDITIONMAX)
                                        {
                                            variable = this.StcTimeLevelMin(rateInfo, skuinfo, info.Lot, info.Qty, ExecuteDate, this.dataBaseID[i]);
                                        }
                                    }
                                    if ((rateInfo.MaxCharge > 0M) && (rateInfo.MaxCharge < variable))
                                    {
                                        variable = rateInfo.MaxCharge;
                                    }
                                    if ((rateInfo.MinCharge > 0M) && (rateInfo.MinCharge > variable))
                                    {
                                        variable = rateInfo.MinCharge;
                                    }
                                    if (rateInfo.CarryMode == RateCarryMode.CM_Round)
                                    {
                                        variable = Math.Round(variable, 2, MidpointRounding.AwayFromZero);
                                    }
                                    if (rateInfo.CarryMode == RateCarryMode.CM_Ceiling)
                                    {
                                        variable = Math.Ceiling(variable);
                                    }
                                    if (rateInfo.CarryMode == RateCarryMode.CM_Floor)
                                    {
                                        variable = Math.Floor(variable);
                                    }
                                    ChargesDetailInfo chargesDetailInfo = new ChargesDetailInfo {
                                        OwnerID = info.OwnerID,
                                        SkuID = info.SkuID,
                                        Qty = info.Qty,
                                        Type = RateChargeType.Inventory,
                                        SourceType = rateInfo.Descr,
                                        RateUnit = rateInfo.ChargeUnit,
                                        IsChecked = "N",
                                        ChargeAmount = variable,
                                        ExecuteDate = Convert.ToDateTime(ExecuteDate),
                                        CreatedBy = "DBA",
                                        UpdatedBy = "DBA"
                                    };
                                    chargesDetailInfoList.Add(chargesDetailInfo);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                    }
                    if ((chargesDetailInfoList.Count > 0) && (this.InserChargesDetail(chargesDetailInfoList, this.dataBaseID[i]) > 0))
                    {
                        temp = this.dataBaseID[i] + ";";
                    }
                }
            }
            return temp;
        }

        public string BillingOperating(string ExecuteDate, List<string> strWhID)
        {
            string temp = string.Empty;
            if ((this.dataBaseID == null) || (this.dataBaseID.Count <= 0))
            {
                throw new Exception("仓储初始化失败！");
            }
            for (int i = 0; this.dataBaseID.Count > i; i++)
            {
                if ((strWhID.Count <= 0) || strWhID.Contains(this.dataBaseID[i]))
                {
                    List<ChargesDetailInfo> chargesDetailInfoList = new List<ChargesDetailInfo>();
                    try
                    {
                        List<InvTransInfo> invTransInfoList = InvTransDA.GetInvTransByExecuteDate(ExecuteDate, this.dataBaseID[i]);
                        if ((invTransInfoList != null) && (invTransInfoList.Count > 0))
                        {
                            foreach (InvTransInfo info in invTransInfoList)
                            {
                                string strTransType = this.ConvertTransType(info.TransType);
                                SkuInfo skuinfo = SkuDA.GetSkuByID(info.OwnerID, info.SkuID, this.dataBaseID[i]);
                                if (skuinfo != null)
                                {
                                    string strRateID = string.Empty;
                                    TariffInfo tariffInfo = TariffDA.GetTariffByID(skuinfo.TariffID, this.dataBaseID[i]);
                                    if (tariffInfo == null)
                                    {
                                        throw new Exception(this.WhConvert(this.dataBaseID, i) + "@货品 " + skuinfo.SkuID + " 未设置价目表!");
                                    }
                                    if (tariffInfo.TariffType == TariffType.CalendarGroup)
                                    {
                                        List<CalendarGroupDetailInfo> calendarGroupDetailInfo = calendarGroupDetailDA.GetCalendarGroupDetailByID(tariffInfo.TypeCode, this.dataBaseID[i]);
                                        if ((calendarGroupDetailInfo == null) || (calendarGroupDetailInfo.Count <= 0))
                                        {
                                            throw new Exception(this.WhConvert(this.dataBaseID, i) + "@未找到货品 " + skuinfo.SkuID + "相关的日历组 编号 " + tariffInfo.TypeCode + " 请核对日历组!");
                                        }
                                        DateTime ExecuteTime = Convert.ToDateTime(ExecuteDate);
                                        foreach (CalendarGroupDetailInfo cgInfo in calendarGroupDetailInfo)
                                        {
                                            if (((cgInfo.StartTime <= ExecuteTime) && (cgInfo.EndTime >= ExecuteTime)) && (cgInfo.IsActive == "Y"))
                                            {
                                                strRateID = cgInfo.RateID;
                                                break;
                                            }
                                        }
                                        if (string.IsNullOrEmpty(strRateID))
                                        {
                                            throw new Exception(this.WhConvert(this.dataBaseID, i) + "@未找到日历组 编号" + tariffInfo.TypeCode + " 内与当前月份相匹配的计费方式!");
                                        }
                                    }
                                    else if (tariffInfo.TariffType == TariffType.Fixed)
                                    {
                                        strRateID = tariffInfo.TypeCode;
                                    }
                                    List<RateDetailInfo> rateDetailInfoList = RateDetailDA.GetRateDetailByID(strRateID, this.dataBaseID[i]);
                                    if ((rateDetailInfoList == null) || (rateDetailInfoList.Count <= 0))
                                    {
                                        throw new Exception(this.WhConvert(this.dataBaseID, i) + "@价目表" + skuinfo.TariffID + " 未找到对应的费率 ");
                                    }
                                    foreach (RateDetailInfo rateInfo in rateDetailInfoList)
                                    {
                                        if ((rateInfo.RateType == strTransType) && (rateInfo.IsActive == "Y"))
                                        {
                                            decimal variable = 0M;
                                            if (rateInfo.ChargeMode == RateBillingMode.Fixed)
                                            {
                                                variable = this.CountChargeUnit(rateInfo.ChargeUnit, info.Qty, skuinfo, rateInfo.ChargePerUnit, this.dataBaseID[i]) * rateInfo.FixedRate;
                                            }
                                            else if (rateInfo.ChargeMode == RateBillingMode.ChargeUnitLevel)
                                            {
                                                if (rateInfo.DistMatchedMode == RateDistMatchedMode.MIN_CONDITION_MAX)
                                                {
                                                    variable = this.ChargeUnitLevelByMax(rateInfo, skuinfo, info.Qty, this.dataBaseID[0]);
                                                }
                                                if (rateInfo.DistMatchedMode == RateDistMatchedMode.MINCONDITIONMAX)
                                                {
                                                    variable = this.ChargeUnitLevelByMin(rateInfo, skuinfo, info.Qty, this.dataBaseID[0]);
                                                }
                                            }
                                            else if (rateInfo.ChargeMode == RateBillingMode.StcTimeLevel)
                                            {
                                                if (rateInfo.DistMatchedMode == RateDistMatchedMode.MIN_CONDITION_MAX)
                                                {
                                                    variable = this.StcTimeLevelMax(rateInfo, skuinfo, info.Lot, info.Qty, ExecuteDate, this.dataBaseID[i]);
                                                }
                                                if (rateInfo.DistMatchedMode == RateDistMatchedMode.MINCONDITIONMAX)
                                                {
                                                    variable = this.StcTimeLevelMin(rateInfo, skuinfo, info.Lot, info.Qty, ExecuteDate, this.dataBaseID[i]);
                                                }
                                            }
                                            if ((rateInfo.MaxCharge > 0M) && (rateInfo.MaxCharge < variable))
                                            {
                                                variable = rateInfo.MaxCharge;
                                            }
                                            if ((rateInfo.MinCharge > 0M) && (rateInfo.MinCharge > variable))
                                            {
                                                variable = rateInfo.MinCharge;
                                            }
                                            if (rateInfo.CarryMode == RateCarryMode.CM_Round)
                                            {
                                                variable = Math.Round(variable);
                                            }
                                            if (rateInfo.CarryMode == RateCarryMode.CM_Ceiling)
                                            {
                                                variable = Math.Ceiling(variable);
                                            }
                                            if (rateInfo.CarryMode == RateCarryMode.CM_Floor)
                                            {
                                                variable = Math.Floor(variable);
                                            }
                                            ChargesDetailInfo chargesDetailInfo = new ChargesDetailInfo {
                                                OwnerID = info.OwnerID,
                                                SkuID = info.SkuID,
                                                Qty = info.Qty,
                                                Type = strTransType,
                                                OrderID = info.DocID,
                                                OrderLineID = info.DocLineID,
                                                SourceType = rateInfo.Descr,
                                                RateUnit = rateInfo.ChargeUnit,
                                                IsChecked = "N",
                                                ChargeAmount = variable,
                                                ExecuteDate = Convert.ToDateTime(ExecuteDate),
                                                CreatedBy = "DBA",
                                                UpdatedBy = "DBA"
                                            };
                                            chargesDetailInfoList.Add(chargesDetailInfo);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                    }
                    if ((chargesDetailInfoList.Count > 0) && (this.InserChargesDetail(chargesDetailInfoList, this.dataBaseID[i]) > 0))
                    {
                        temp = temp + this.dataBaseID[i] + ";";
                    }
                }
            }
            return "";
        }

        private decimal ChargeUnitLevelByMax(RateDetailInfo rateInfo, SkuInfo skuinfo, decimal Qty, string WhLoginID)
        {
            decimal variable = 0M;
            Qty = Math.Abs(Qty);
            if (!(rateInfo.ChargeMode == RateBillingMode.ChargeUnitLevel))
            {
                return variable;
            }
            variable = this.CountChargeUnit(rateInfo.ChargeUnit, Qty, skuinfo, rateInfo.ChargePerUnit, WhLoginID);
            if ((rateInfo.CucFrom01 < variable) && (variable <= rateInfo.CucTo01))
            {
                return (variable * rateInfo.CucRate01);
            }
            if (((rateInfo.CucFrom02 != 0) && (rateInfo.CucFrom02 < variable)) && ((variable <= rateInfo.CucTo02) || (rateInfo.CucTo02 == 0)))
            {
                return (variable * rateInfo.CucRate02);
            }
            if (((rateInfo.CucFrom03 != 0) && (rateInfo.CucFrom03 < variable)) && ((variable <= rateInfo.CucTo03) || (rateInfo.CucTo03 == 0)))
            {
                return (variable * rateInfo.CucRate03);
            }
            if (((rateInfo.CucFrom04 != 0) && (rateInfo.CucFrom04 < variable)) && ((variable <= rateInfo.CucTo04) || (rateInfo.CucTo04 == 0)))
            {
                return (variable * rateInfo.CucRate04);
            }
            if (((rateInfo.CucFrom05 != 0) && (rateInfo.CucFrom05 < variable)) && ((variable <= rateInfo.CucTo05) || (rateInfo.CucTo05 == 0)))
            {
                return (variable * rateInfo.CucRate05);
            }
            if (((rateInfo.CucFrom06 != 0) && (rateInfo.CucFrom06 < variable)) && ((variable <= rateInfo.CucTo06) || (rateInfo.CucTo06 == 0)))
            {
                return (variable * rateInfo.CucRate06);
            }
            if (((rateInfo.CucFrom07 != 0) && (rateInfo.CucFrom07 < variable)) && ((variable <= rateInfo.CucTo07) || (rateInfo.CucTo07 == 0)))
            {
                return (variable * rateInfo.CucRate07);
            }
            if (((rateInfo.CucFrom08 != 0) && (rateInfo.CucFrom08 < variable)) && ((variable <= rateInfo.CucTo08) || (rateInfo.CucTo08 == 0)))
            {
                return (variable * rateInfo.CucRate08);
            }
            if (((rateInfo.CucFrom09 != 0) && (rateInfo.CucFrom09 < variable)) && ((variable <= rateInfo.CucTo09) || (rateInfo.CucFrom09 == 0)))
            {
                return (variable * rateInfo.CucRate09);
            }
            if (((rateInfo.CucFrom10 == 0) || (rateInfo.CucFrom10 >= variable)) || ((variable > rateInfo.CucTo10) && (rateInfo.CucTo10 != 0)))
            {
                throw new Exception("货品" + skuinfo.SkuID + "在计费单位分级中未找到满足条件的区间！");
            }
            return (variable * rateInfo.CucRate10);
        }

        private decimal ChargeUnitLevelByMin(RateDetailInfo rateInfo, SkuInfo skuinfo, decimal Qty, string WhLoginID)
        {
            decimal variable = 0M;
            Qty = Math.Abs(Qty);
            if (!(rateInfo.ChargeMode == RateBillingMode.ChargeUnitLevel))
            {
                return variable;
            }
            variable = this.CountChargeUnit(rateInfo.ChargeUnit, Qty, skuinfo, rateInfo.ChargePerUnit, WhLoginID);
            if ((rateInfo.CucFrom01 <= variable) && (variable < rateInfo.CucTo01))
            {
                return (variable * rateInfo.CucRate01);
            }
            if (((rateInfo.CucFrom02 != 0) && (rateInfo.CucFrom02 <= variable)) && ((variable < rateInfo.CucTo02) || (rateInfo.CucTo02 == 0)))
            {
                return (variable * rateInfo.CucRate02);
            }
            if (((rateInfo.CucFrom03 != 0) && (rateInfo.CucFrom03 <= variable)) && ((variable < rateInfo.CucTo03) || (rateInfo.CucTo03 == 0)))
            {
                return (variable * rateInfo.CucRate03);
            }
            if (((rateInfo.CucFrom04 != 0) && (rateInfo.CucFrom04 <= variable)) && ((variable < rateInfo.CucTo04) || (rateInfo.CucTo04 == 0)))
            {
                return (variable * rateInfo.CucRate04);
            }
            if (((rateInfo.CucFrom05 != 0) && (rateInfo.CucFrom05 <= variable)) && ((variable < rateInfo.CucTo05) || (rateInfo.CucTo05 == 0)))
            {
                return (variable * rateInfo.CucRate05);
            }
            if (((rateInfo.CucFrom06 != 0) && (rateInfo.CucFrom06 <= variable)) && ((variable < rateInfo.CucTo06) || (rateInfo.CucTo06 == 0)))
            {
                return (variable * rateInfo.CucRate06);
            }
            if (((rateInfo.CucFrom07 != 0) && (rateInfo.CucFrom07 <= variable)) && ((variable < rateInfo.CucTo07) || (rateInfo.CucTo07 == 0)))
            {
                return (variable * rateInfo.CucRate07);
            }
            if (((rateInfo.CucFrom08 != 0) && (rateInfo.CucFrom08 <= variable)) && ((variable < rateInfo.CucTo08) || (rateInfo.CucTo08 == 0)))
            {
                return (variable * rateInfo.CucRate08);
            }
            if (((rateInfo.CucFrom09 != 0) && (rateInfo.CucFrom09 <= variable)) && ((variable < rateInfo.CucTo09) || (rateInfo.CucFrom09 == 0)))
            {
                return (variable * rateInfo.CucRate09);
            }
            if (((rateInfo.CucFrom10 == 0) || (rateInfo.CucFrom10 > variable)) || ((variable >= rateInfo.CucTo10) && (rateInfo.CucTo10 != 0)))
            {
                throw new Exception("货品" + skuinfo.SkuID + "在计费单位分级中未找到满足条件的区间！");
            }
            return (variable * rateInfo.CucRate10);
        }

        private string ConvertTransType(string TransType)
        {
            string strtemp = string.Empty;
            string CS40001 = TransType;
            if (CS40001 == null)
            {
                return strtemp;
            }
            if (!(CS40001 == "IN"))
            {
                if (((CS40001 == "AJ") || (CS40001 == "MV")) || (CS40001 != "OT"))
                {
                    return strtemp;
                }
            }
            else
            {
                return RateChargeType.InBound;
            }
            return RateChargeType.OutBound;
        }

        public decimal CostAccountBySku(string OwnerID, string SkuID, decimal Qty, string Lot, string rateID, string TransType, string WhLoginID)
        {
            DateTime ExecuteTime = DateTime.Now;
            decimal variable = 0M;
            SkuInfo skuinfo = SkuDA.GetSkuByID(OwnerID, SkuID, WhLoginID);
            if (skuinfo == null)
            {
                throw new Exception(WhLoginID + "未找到货主" + OwnerID + " 相关货品" + SkuID + ",业务组织失败!");
            }
            string strRateID = string.Empty;
            if (!string.IsNullOrEmpty(rateID))
            {
                strRateID = rateID;
            }
            else
            {
                TariffInfo tariffInfo = TariffDA.GetTariffByID(skuinfo.TariffID, WhLoginID);
                if (tariffInfo == null)
                {
                    throw new Exception(WhLoginID + "货品 " + skuinfo.SkuID + " 未设置价目表!");
                }
                if (!(tariffInfo.TariffType == TariffType.CalendarGroup))
                {
                    if (tariffInfo.TariffType == TariffType.Fixed)
                    {
                        strRateID = tariffInfo.TypeCode;
                    }
                }
                else
                {
                    List<CalendarGroupDetailInfo> calendarGroupDetailInfo = calendarGroupDetailDA.GetCalendarGroupDetailByID(tariffInfo.TypeCode, WhLoginID);
                    if ((calendarGroupDetailInfo == null) || (calendarGroupDetailInfo.Count <= 0))
                    {
                        throw new Exception(WhLoginID + " 未找到货品 " + skuinfo.SkuID + "相关的日历组 编号 " + tariffInfo.TypeCode + " 请核对日历组!");
                    }
                    foreach (CalendarGroupDetailInfo cgInfo in calendarGroupDetailInfo)
                    {
                        if (((cgInfo.StartTime <= ExecuteTime) && (cgInfo.EndTime >= ExecuteTime)) && (cgInfo.IsActive == "Y"))
                        {
                            strRateID = cgInfo.RateID;
                            break;
                        }
                    }
                    if (string.IsNullOrEmpty(strRateID))
                    {
                        throw new Exception(WhLoginID + "未找到日历组 编号" + tariffInfo.TypeCode + "内与当前月份相匹配的计费方式! ");
                    }
                }
            }
            List<RateDetailInfo> rateDetailInfoList = RateDetailDA.GetRateDetailByID(strRateID, WhLoginID);
            if ((rateDetailInfoList == null) || (rateDetailInfoList.Count <= 0))
            {
                throw new Exception(WhLoginID + "价目表" + skuinfo.TariffID + " 未找到对应的费率 ");
            }
            foreach (RateDetailInfo rateInfo in rateDetailInfoList)
            {
                if ((rateInfo.RateType == TransType) && (rateInfo.IsActive == "Y"))
                {
                    if (rateInfo.ChargeMode == RateBillingMode.Fixed)
                    {
                        variable = this.CountChargeUnit(rateInfo.ChargeUnit, Qty, skuinfo, rateInfo.ChargePerUnit, WhLoginID) * rateInfo.FixedRate;
                    }
                    else if (rateInfo.ChargeMode == RateBillingMode.ChargeUnitLevel)
                    {
                        if (rateInfo.DistMatchedMode == RateDistMatchedMode.MIN_CONDITION_MAX)
                        {
                            variable = this.ChargeUnitLevelByMax(rateInfo, skuinfo, Qty, WhLoginID);
                        }
                        if (rateInfo.DistMatchedMode == RateDistMatchedMode.MINCONDITIONMAX)
                        {
                            variable = this.ChargeUnitLevelByMin(rateInfo, skuinfo, Qty, WhLoginID);
                        }
                    }
                    else if (rateInfo.ChargeMode == RateBillingMode.StcTimeLevel)
                    {
                        if (rateInfo.DistMatchedMode == RateDistMatchedMode.MIN_CONDITION_MAX)
                        {
                            variable = this.StcTimeLevelMax(rateInfo, skuinfo, Lot, Qty, ExecuteTime.ToString("yyyy-MM-dd"), WhLoginID);
                        }
                        if (rateInfo.DistMatchedMode == RateDistMatchedMode.MINCONDITIONMAX)
                        {
                            variable = this.StcTimeLevelMin(rateInfo, skuinfo, Lot, Qty, ExecuteTime.ToString("yyyy-MM-dd"), WhLoginID);
                        }
                    }
                    if ((rateInfo.MaxCharge > 0M) && (rateInfo.MaxCharge < variable))
                    {
                        variable = rateInfo.MaxCharge;
                    }
                    if ((rateInfo.MinCharge > 0M) && (rateInfo.MinCharge > variable))
                    {
                        variable = rateInfo.MinCharge;
                    }
                    if (rateInfo.CarryMode == RateCarryMode.CM_Round)
                    {
                        variable = Math.Round(variable);
                    }
                    if (rateInfo.CarryMode == RateCarryMode.CM_Ceiling)
                    {
                        variable = Math.Ceiling(variable);
                    }
                    if (rateInfo.CarryMode == RateCarryMode.CM_Floor)
                    {
                        variable = Math.Floor(variable);
                    }
                }
            }
            return variable;
        }

        private decimal CountChargeUnit(string ChargeUnit, decimal Qty, SkuInfo skuinfo, decimal ChargePerUnit, string WhLoginID)
        {
            Qty = Math.Abs(Qty);
            if (ChargeUnit == RateChargeUnit.Weight)
            {
                return ((Convert.ToDecimal(skuinfo.GrossWgt) * Qty) / ChargePerUnit);
            }
            if (ChargeUnit == RateChargeUnit.Area)
            {
                return ((Convert.ToDecimal((float) (skuinfo.Length * skuinfo.Width)) * Qty) / ChargePerUnit);
            }
            if (ChargeUnit == RateChargeUnit.Measurement)
            {
                return ((Convert.ToDecimal(skuinfo.Cube) * Qty) / ChargePerUnit);
            }
            if (ChargeUnit == RateChargeUnit.AMT)
            {
                return ((Convert.ToDecimal(skuinfo.CostPrice) * Qty) / ChargePerUnit);
            }
            if (ChargeUnit == RateChargeUnit.Quantity)
            {
                return (Qty / ChargePerUnit);
            }
            if (ChargeUnit == RateChargeUnit.BillingTon)
            {
                if (skuinfo.GrossWgt > skuinfo.Cube)
                {
                    return ((Convert.ToDecimal(skuinfo.GrossWgt) * Qty) / ChargePerUnit);
                }
                return ((Convert.ToDecimal(skuinfo.Cube) * Qty) / ChargePerUnit);
            }
            if (ChargeUnit == RateChargeUnit.Pallet)
            {
                PackInfo packInfo = iPackDA.GetPackByID(skuinfo.PackID, WhLoginID);
                if (packInfo == null)
                {
                    throw new Exception("货品" + skuinfo.SkuID + " 未找到匹配的包装代码!");
                }
                return (Math.Ceiling((decimal) (Qty / Convert.ToDecimal(packInfo.Pallet))) / ChargePerUnit);
            }
            if (ChargeUnit != RateChargeUnit.FixedRate)
            {
                throw new Exception("货品" + skuinfo.SkuID + " 未找到匹配的费率类型!");
            }
            return 1M;
        }

        public int InserChargesDetail(List<ChargesDetailInfo> chargesDetailInfoList, string WhLoginID)
        {
            int temp = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                foreach (ChargesDetailInfo cdInfo in chargesDetailInfoList)
                {
                    temp += chargesDetailDA.InsertChargesDetail(dataBase, tran, cdInfo, WhLoginID);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        private decimal StcTimeLevelMax(RateDetailInfo rateInfo, SkuInfo skuinfo, string Lot, decimal Qty, string ExecuteDate, string WhLoginID)
        {
            decimal variable = 0M;
            decimal day = 0M;
            Qty = Math.Abs(Qty);
            LotAttributeInfo lotAttributeInfo = LotAtributeDA.GetLotAttributeByID(Lot, WhLoginID);
            if (lotAttributeInfo == null)
            {
                return variable;
            }
            variable = this.CountChargeUnit(rateInfo.ChargeUnit, Qty, skuinfo, rateInfo.ChargePerUnit, WhLoginID);
            TimeSpan ts = (TimeSpan) (lotAttributeInfo.ReceiptDate - Convert.ToDateTime(ExecuteDate));
            day = ts.Days;
            if ((rateInfo.StcFrom01 < day) && (day <= rateInfo.StcTo01))
            {
                return (variable * rateInfo.StcRate01);
            }
            if (((rateInfo.StcFrom02 != 0) && (rateInfo.StcFrom02 < day)) && ((day <= rateInfo.StcTo02) || (rateInfo.StcTo02 == 0)))
            {
                return (variable * rateInfo.StcRate02);
            }
            if (((rateInfo.StcFrom03 != 0) && (rateInfo.StcFrom03 < day)) && ((day <= rateInfo.StcTo03) || (rateInfo.StcTo03 == 0)))
            {
                return (variable * rateInfo.StcRate03);
            }
            if (((rateInfo.StcFrom04 != 0) && (rateInfo.StcFrom04 < day)) && ((day <= rateInfo.StcTo04) || (rateInfo.StcTo04 == 0)))
            {
                return (variable * rateInfo.StcRate04);
            }
            if (((rateInfo.StcFrom05 != 0) && (rateInfo.StcFrom05 < day)) && ((day <= rateInfo.StcTo05) || (rateInfo.StcTo05 == 0)))
            {
                return (variable * rateInfo.StcRate05);
            }
            if (((rateInfo.StcFrom06 != 0) && (rateInfo.StcFrom06 < day)) && ((day <= rateInfo.StcTo06) || (rateInfo.StcTo06 == 0)))
            {
                return (variable * rateInfo.StcRate06);
            }
            if (((rateInfo.StcFrom07 != 0) && (rateInfo.StcFrom07 < day)) && ((day <= rateInfo.StcTo07) || (rateInfo.StcTo07 == 0)))
            {
                return (variable * rateInfo.StcRate07);
            }
            if (((rateInfo.StcFrom08 != 0) && (rateInfo.StcFrom08 < day)) && ((day <= rateInfo.StcTo08) || (rateInfo.StcTo08 == 0)))
            {
                return (variable * rateInfo.StcRate08);
            }
            if (((rateInfo.StcFrom09 != 0) && (rateInfo.StcFrom09 < day)) && ((day <= rateInfo.StcTo09) || (rateInfo.StcFrom09 == 0)))
            {
                return (variable * rateInfo.StcRate09);
            }
            if (((rateInfo.StcFrom10 == 0) || (rateInfo.StcFrom10 >= day)) || ((day > rateInfo.StcTo10) && (rateInfo.StcTo10 != 0)))
            {
                throw new Exception("存储时间分级中未找到满足条件的区间");
            }
            return (variable * rateInfo.StcRate10);
        }

        private decimal StcTimeLevelMin(RateDetailInfo rateInfo, SkuInfo skuinfo, string Lot, decimal Qty, string ExecuteDate, string WhLoginID)
        {
            decimal variable = 0M;
            decimal day = 0M;
            Qty = Math.Abs(Qty);
            LotAttributeInfo lotAttributeInfo = LotAtributeDA.GetLotAttributeByID(Lot, WhLoginID);
            if (lotAttributeInfo == null)
            {
                return variable;
            }
            variable = this.CountChargeUnit(rateInfo.ChargeUnit, Qty, skuinfo, rateInfo.ChargePerUnit, WhLoginID);
            TimeSpan ts = (TimeSpan) (lotAttributeInfo.ReceiptDate - Convert.ToDateTime(ExecuteDate));
            day = ts.Days;
            if ((rateInfo.StcFrom01 <= day) && (day < rateInfo.StcTo01))
            {
                return (variable * rateInfo.StcRate01);
            }
            if (((rateInfo.StcFrom02 != 0) && (rateInfo.StcFrom02 <= day)) && ((day < rateInfo.StcTo02) || (rateInfo.StcTo02 == 0)))
            {
                return (variable * rateInfo.StcRate02);
            }
            if (((rateInfo.StcFrom03 != 0) && (rateInfo.StcFrom03 <= day)) && ((day < rateInfo.StcTo03) || (rateInfo.StcTo03 == 0)))
            {
                return (variable * rateInfo.StcRate03);
            }
            if (((rateInfo.StcFrom04 != 0) && (rateInfo.StcFrom04 <= day)) && ((day < rateInfo.StcTo04) || (rateInfo.StcTo04 == 0)))
            {
                return (variable * rateInfo.StcRate04);
            }
            if (((rateInfo.StcFrom05 != 0) && (rateInfo.StcFrom05 <= day)) && ((day < rateInfo.StcTo05) || (rateInfo.StcTo05 == 0)))
            {
                return (variable * rateInfo.StcRate05);
            }
            if (((rateInfo.StcFrom06 != 0) && (rateInfo.StcFrom06 <= day)) && ((day < rateInfo.StcTo06) || (rateInfo.StcTo06 == 0)))
            {
                return (variable * rateInfo.StcRate06);
            }
            if (((rateInfo.StcFrom07 != 0) && (rateInfo.StcFrom07 <= day)) && ((day < rateInfo.StcTo07) || (rateInfo.StcTo07 == 0)))
            {
                return (variable * rateInfo.StcRate07);
            }
            if (((rateInfo.StcFrom08 != 0) && (rateInfo.StcFrom08 <= day)) && ((day < rateInfo.StcTo08) || (rateInfo.StcTo08 == 0)))
            {
                return (variable * rateInfo.StcRate08);
            }
            if (((rateInfo.StcFrom09 != 0) && (rateInfo.StcFrom09 <= day)) && ((day < rateInfo.StcTo09) || (rateInfo.StcFrom09 == 0)))
            {
                return (variable * rateInfo.StcRate09);
            }
            if (((rateInfo.StcFrom10 == 0) || (rateInfo.StcFrom10 > day)) || ((day >= rateInfo.StcTo10) && (rateInfo.StcTo10 != 0)))
            {
                throw new Exception("存储时间分级中未找到满足条件的区间");
            }
            return (variable * rateInfo.StcRate10);
        }

        private string WhConvert(List<string> dataBaseID, int i)
        {
            string StrWh = string.Empty;
            for (int n = i; dataBaseID.Count > n; n++)
            {
                StrWh = StrWh + dataBaseID[n] + ";";
            }
            return StrWh;
        }
    }
}

